package com.system.server;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

import java.sql.ResultSet;
import java.sql.SQLException;

public class FoodUI extends Application {
    public void start(Stage stage)
    {
        GridPane pane=new GridPane();
        pane.setAlignment(Pos.BASELINE_CENTER);
        pane.setPadding(new Insets(50,50,50,50));
        pane.setHgap(10);
        pane.setVgap(10);

        Label TitleLabel=new Label("添加商品");
        TitleLabel.setFont(Font.font("添加商品", FontWeight.BOLD,20));
        pane.add(TitleLabel,0,0);
        Label foodName_label=new Label("名称: ");
        pane.add(foodName_label,0,1);
        TextField food_name=new TextField();
        pane.add(food_name,1,1);
        Label foodValue_label=new Label("价格: ");
        pane.add(foodValue_label,0,2);
        TextField food_value=new TextField();
        pane.add(food_value,1,2);
        Label foodKind_label=new Label("种类: ");
        pane.add(foodKind_label,0,3);
        TextField food_kind=new TextField();
        pane.add(food_kind,1,3);
        Label foodNum_label=new Label("库存: ");
        pane.add(foodNum_label,0,4);
        TextField food_number=new TextField();
        pane.add(food_number,1,4);
        Label foodSale_label=new Label("销量: ");
        pane.add(foodSale_label,0,5);
        TextField food_sale=new TextField();
        pane.add(food_sale,1,5);
        Label foodInfo_label=new Label("简介: ");
        pane.add(foodInfo_label,0,6);
        TextField food_Info=new TextField();
        pane.add(food_Info,1,6);
        Label foodUrl_label=new Label("图片: ");
        pane.add(foodUrl_label,0,7);
        TextField food_url=new TextField();
        pane.add(food_url,1,7);

        Button addBtn=new Button("添加");
        Button exitBtn=new Button("退出");
        pane.add(addBtn,0,8);
        pane.add(exitBtn,1,8);
        /*
        pane.add(deleteBtn,1,8);
        Button updateBtn=new Button("修改");
        pane.add(updateBtn,0,9);
        Button selectBtn=new Button("查询");
        pane.add(selectBtn,1,9);

         */




        addBtn.setOnAction(actionEvent -> {
            Food food=new Food();
            food.setFood_name(food_name.getText());
            food.setFood_value(food_value.getText());
            food.setFood_kind(food_kind.getText());
            food.setFood_number(food_number.getText());
            food.setFood_sale(food_sale.getText());
            food.setFood_info(food_Info.getText());
            food.setFood_url(food_url.getText());
            FoodDAO.insertFood(food);
            food_name.clear();
            food_value.clear();
            food_kind.clear();
            food_number.clear();
            food_sale.clear();
            food_Info.clear();
            food_url.clear();
        });

        exitBtn.setOnAction(actionEvent -> {
            stage.close();
        });

        /*
        deleteBtn.setOnAction(actionEvent -> {
            String foodName=food_name.getText();
            FoodDAO.deleteFood(foodName);
            food_name.clear();
        });

        selectBtn.setOnAction(actionEvent -> {
            String foodName=food_name.getText();
            ResultSet res=FoodDAO.selectWithFoodName(foodName);
            try{
                while(res.next()){
                    food_value.setText(res.getString("food_value"));
                    food_kind.setText(res.getString("food_kind"));
                    food_number.setText(res.getString("food_number"));
                    food_sale.setText(res.getString("food_sale"));
                    food_Info.setText(res.getString("food_info"));
                    food_url.setText(res.getString("food_url"));
                    return;
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
            food_name.setText("查询失败，不存在该商品");
        });

        updateBtn.setOnAction(actionEvent -> {
            Food food=new Food();
            food.setFood_name(food_name.getText());
            food.setFood_value(food_value.getText());
            food.setFood_kind(food_kind.getText());
            food.setFood_number(food_number.getText());
            food.setFood_sale(food_sale.getText());
            food.setFood_info(food_Info.getText());
            food.setFood_url(food_url.getText());
            FoodDAO.updateValue(food.getFood_name(),food.getFood_value());
            FoodDAO.updateKind(food.getFood_name(),food.getFood_kind());
            String sql1="update `food` set food_sale='"+food.getFood_sale()+"'where food_name='"+food.getFood_name()+"'";
            DataBaseControl.execute(sql1);
            String sql2="update `food` set food_number='"+food.getFood_number()+"'where food_name='"+food.getFood_name()+"'";
            DataBaseControl.execute(sql2);
            FoodDAO.updateInfo(food.getFood_name(),food.getFood_info());
        });
        */


        Scene scene=new Scene(pane);
        stage.setScene(scene);
        stage.setTitle("添加商品");
        stage.show();

    }


}
